package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品静态页面
 */
@Service
public class PageServiceImpl implements PageService {
    //获取配置文件中的内容
    @Value("${pagepath}")
    private String pagepath;

    //调用goods功能
    @Autowired
    private SkuFeign skuFeign;

    //静态化页面生成引擎
    @Autowired
    private TemplateEngine templateEngine;


    public void generateHtml(String spuId) {
        //1.获取context对象,用于存储商品的相关数据
        Context context = new Context();

        //1.1获取静态化页面的相关数据
        Map<String, Object> itemData = this.getItemData(spuId);

        //1.2
        context.setVariables(itemData);

        //2.获取商品详情页面的存储位置
        File dir = new File(pagepath);

        //3.判断当前存储位置的文件夹是否存在,如果不存在则新建
        if (!dir.exists()) {//exists判断文件是否存在
            //不存在
            dir.mkdirs();
        }
        //4.定义输出流,完成文件的生成
        File file = new File(dir + "/" + spuId + ".html");

        Writer out = null;

        try {
            out = new PrintWriter(file);

            // 4.1生成静态化页面
            /*
             * 1.模板名称
             *
             * 2.context
             *
             * 3.输出流

             * */

            templateEngine.process("item", context, out);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5.关闭流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


    //私有方法获取静态化页面的相关数据
    private Map<String, Object> getItemData(String spuId) {
        Map<String, Object> resultMap = new HashMap<>();

        //1.获取spu
        Result<Spu> spuById = skuFeign.findSpuById(spuId);
        Spu spu = spuById.getData();
        resultMap.put("spu", spu);

        //2.获取图片信息
        if (spu != null) {
            if (StringUtils.isNotEmpty(spu.getImages())) {
                //2.1图片多,要进行切割(,)
                resultMap.put("imageList", spu.getImages().split(","));
            }
        }

        //3.获取商品的分类信息
        //3.1获取商品一级分类对象
        Category category1 = skuFeign.findById(spu.getCategory1Id()).getData();
        resultMap.put("category1", category1);

        //3.2获取商品二级分类对象
        Category category2 = skuFeign.findById(spu.getCategory2Id()).getData();
        resultMap.put("category2", category2);

        //3.3获取商品三级分类对象
        Category category3 = skuFeign.findById(spu.getCategory3Id()).getData();
        resultMap.put("category3", category3);

        //4.获取sku的相关信息
        List<Sku> skuList = skuFeign.findSkuListSpuId(spuId);
        resultMap.put("skuList", skuList);

        //5.获取商品的规格信息
        //5.1得到商品规格信息
        String specItems = spu.getSpecItems();

        //5.2由于是JSON格式,所以需要转为Map格式
        Map specificationList = JSON.parseObject(specItems, Map.class);

        //5.3添加到Map中
        resultMap.put("specificationList", specificationList);

        return resultMap;
    }
}
